/*
This file is part of La Linea.

La Linea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

La Linea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with La Linea.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#ifndef FRAME_H
#define FRAME_H

#include "globals.h"

#include "FrameCacheData.h"
#include "FrameStencil.h"
#include "OneLineCurve.h"
#include "Rectangle.h"

#include <fstream>
#include <vector>
#include <string>

class FrameCacheData;

// Describe a Frame
class Frame
{
private:
	std::vector<OneLineCurve*> _lines;
	std::vector<FrameStencil*> _stencilPos;
	std::vector<FrameStencil*> _stencilNeg;
	FrameCacheData* _cache;
	RectangleDesc _boundingBox;
	bool _validBoundingBox;
	int _refCounter; // Count the number of references to that Frame

public:
	Frame(void);
	~Frame(void);
	
	// Return a copy of the Frame
	Frame* copy();

	// Get the OneLineCurves constituting the Frame
	std::vector<OneLineCurve*>& getLines();
	
	// Get positive stencils
	std::vector<FrameStencil*>& getStencilPos();
	
	// Get negative stencils
	std::vector<FrameStencil*>& getStencilNeg();

	// Add a OneLineCurve to the Frame
	void addLine(OneLineCurve* c);

	// Remove a OneLineCurve from the Frame
	void removeLine(OneLineCurve* c);

	// Remove a Node from the Frame without breaking everything
	void deleteNode(Node* n);

	// Draw the Frame
	void draw(GLfloat colorr = 1.0, GLfloat colorg = 1.0, GLfloat colorb = 1.0, GLfloat colora = 1.0, unsigned int options = DRAW_BORDERLINE);

	// Draw stencils
	void drawStencils(unsigned int options = 0);
	
	// Draw bounding box
	void drawBoundingBox();
	
	// Create a display list from the Frame and return its ID
	GLuint createDisplayList();

	// Generate the Frame's bounding box
	RectangleDesc getBoundingBox();

	// Get cache data if it exists or NULL otherwise
	FrameCacheData* getCache();

	// Set cache attribute
	void setCache(FrameCacheData* c);
	
	// Tesselate stencils
	void tesselateStencils();
	
	// Add a positive stencil
	void addPosStencil(FrameStencil* s);
	
	// Add a negative stencil
	void addNegStencil(FrameStencil* s);
	
	// Delete stencils
	void deleteStencils();
	
	// Retrieve a control node given two origin nodes
	Node* getControlNode(Node* n1, Node* n2);

	// Increments _refCounter and returns its new value
	int incRefCounter();

	// Decrements _refCounter and returns its new value
	int decRefCounter();

	// Returns _refCounter's value
	int getRefCounter();
};

#endif
